<?php

namespace apps\marriage\app\op\controller;

use apps\marriage\core\model\UserAccountBook;
use apps\marriage\core\service\MatchmakerSpreadSalaryRecordService;
use apps\marriage\core\service\MemberProfileService;
use apps\marriage\core\service\UserAccountBookService;
use apps\marriage\core\service\UserService;
use think\Request;

class MatchmakerController extends BaseMarriageOpController
{
    /**
     * @var UserService
     */
    private $userService;

    /**
     * @var MatchmakerSpreadSalaryRecordService
     */
    private $matchmakerSpreadSalaryRecordService;

    /**
     * @var UserAccountBookService
     */
    private $userAccountBookService;

    /**
     * @var MemberProfileService
     */
    private $memberProfileService;

    protected function init()
    {
        parent::init();
        $this->memberProfileService = service('MemberProfile', SERVICE_NAMESPACE);
        $this->userAccountBookService = service('UserAccountBook', SERVICE_NAMESPACE);
        $this->userService = service('User', SERVICE_NAMESPACE);
        $this->matchmakerSpreadSalaryRecordService = service('MatchmakerSpreadSalaryRecord', SERVICE_NAMESPACE);
    }

    public function lists(Request $request)
    {
        $pageSize = $request->param('page_size', 10, 'intval');
        $keywords = $request->param('keywords', '');
        $params = [
            'is_matchmaker' => 1
        ];
        if(!empty($keywords)) {
            $params['keywords'] = $keywords;
        }
        $res = $this->userService->pageListByParams($params, $pageSize);
        foreach ($res['data'] as &$rs) {
            $rs['nick'] = strip_tags($rs['nick']);
            $rs['avatar_url'] = $this->userService->getAvatarUrl($rs['avatar'], $rs['id']);
            $fans['first'] = $this->userService->count(['fromuid' => $rs['id']]);
            $fans['second'] = $this->userService->count(['matchmaker_uid' => $rs['id']]) - $fans['first'];
            $salary['all'] = $this->matchmakerSpreadSalaryRecordService->sum('amount', ['user_id' => $rs['id']]);
            $salary['today'] = $this->matchmakerSpreadSalaryRecordService->sum('amount', [
                'user_id' => $rs['id'],
                'begin_time' => strtotime(date('Ymd 00:00:00')),
                'end_time' => strtotime(date('Ymd 23:59:59')),
            ]);
            $nowtime  = time();
            $salary['yesterday'] = $this->matchmakerSpreadSalaryRecordService->sum('amount', [
                'user_id' => $rs['id'],
                'begin_time' => strtotime(date('Ymd 00:00:00', $nowtime - 24*3600)),
                'end_time' => strtotime(date('Ymd 23:59:59', $nowtime - 24*3600)),
            ]);
            $salary['last7days'] = $this->matchmakerSpreadSalaryRecordService->sum('amount', [
                'user_id' => $rs['id'],
                'begin_time' => strtotime(date('Ymd 00:00:00', $nowtime - 24*3600*7)),
                'end_time' => strtotime(date('Ymd 23:59:59')),
            ]);
            $rs['fans'] = $fans;
            $rs['salary'] = $salary;
        }
        return $this->success($res);
    }

    public function terminate(Request $request)
    {
        $id = $request->param('id', 0, 'intval');
        $this->userService->onTerminateMatchmaker($id);
        return $this->success();
    }

    public function salary(Request $request)
    {
        $pageSize = $request->param('page_size', 10);
        $userId = $request->param('user_id', 0, 'intval');
        $params = [
            'user_id' => $userId
        ];
        $res = $this->matchmakerSpreadSalaryRecordService->pageListByParams($params, $pageSize);
        if($res['data']) {
            foreach ($res['data'] as &$rs) {
                $rs = $this->matchmakerSpreadSalaryRecordService->format($rs);
            }
        }
        return $this->success($res);
    }

    public function recommend(Request $request)
    {
        $pageSize = $request->param('page_size', 10);
        $userId = $request->param('user_id', 0, 'intval');
        $params = [
            'matchmaker_uid' => $userId
        ];
        $res = $this->userService->pageListByParams($params, $pageSize);
        if($res['data']) {
            $dataList = [];
            foreach ($res['data'] as $rs) {
                $rs['avatar_url'] = $this->userService->getAvatarUrl($rs['avatar'], $rs['id']);
                $rs = arrayOnly($rs, ['id', 'nick', 'avatar_url', 'matchmaker_uids', 'register_time', 'last_login_time']);
                $member = $this->memberProfileService->getByUserId($rs['id']);
                $member = $this->memberProfileService->format($member);
                if(empty($rs['avatar_url'])) {
                    $rs['avatar_url'] = $member['photo_url'];
                }
                $dataList[] = [
                    'user' => $rs,
                    'member' => $member,
                    'expend_amount' => $this->userAccountBookService->sumAmount($rs['id'], UserAccountBook::OP_DECREASE)
                ];
            }
            $res['data'] = $dataList;
        }
        return $this->success($res);
    }
}